// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.modding;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import zombie.core.Core;
import zombie.core.logger.ExceptionLogger;
import zombie.core.math.PZMath;
import zombie.scripting.ScriptParser;
import zombie.util.StringUtils;

public final class ActiveModsFile {
    private static final int VERSION1 = 1;
    private static final int VERSION = 1;

    public boolean write(String path, ActiveMods activeMods) {
        if (Core.getInstance().isNoSave()) {
            return false;
        } else {
            File file = new File(path);

            try {
                FileWriter fileWriter = new FileWriter(file);

                try {
                    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

                    try {
                        String string = this.toString(activeMods);
                        bufferedWriter.write(string);
                    } catch (Throwable throwable0) {
                        try {
                            bufferedWriter.close();
                        } catch (Throwable throwable1) {
                            throwable0.addSuppressed(throwable1);
                        }

                        throw throwable0;
                    }

                    bufferedWriter.close();
                } catch (Throwable throwable2) {
                    try {
                        fileWriter.close();
                    } catch (Throwable throwable3) {
                        throwable2.addSuppressed(throwable3);
                    }

                    throw throwable2;
                }

                fileWriter.close();
                return true;
            } catch (Exception exception) {
                ExceptionLogger.logException(exception);
                return false;
            }
        }
    }

    private String toString(ActiveMods activeMods) {
        ScriptParser.Block block0 = new ScriptParser.Block();
        block0.setValue("VERSION", String.valueOf(1));
        ScriptParser.Block block1 = block0.addBlock("mods", null);
        ArrayList arrayList0 = activeMods.getMods();

        for (int int0 = 0; int0 < arrayList0.size(); int0++) {
            block1.addValue("mod", (String)arrayList0.get(int0));
        }

        ScriptParser.Block block2 = block0.addBlock("maps", null);
        ArrayList arrayList1 = activeMods.getMapOrder();

        for (int int1 = 0; int1 < arrayList1.size(); int1++) {
            block2.addValue("map", (String)arrayList1.get(int1));
        }

        StringBuilder stringBuilder = new StringBuilder();
        String string = System.lineSeparator();
        block0.prettyPrintElements(0, stringBuilder, string);
        return stringBuilder.toString();
    }

    public boolean read(String path, ActiveMods activeMods) {
        activeMods.clear();

        try {
            FileReader fileReader = new FileReader(path);

            try {
                BufferedReader bufferedReader = new BufferedReader(fileReader);

                try {
                    StringBuilder stringBuilder = new StringBuilder();

                    for (String string = bufferedReader.readLine(); string != null; string = bufferedReader.readLine()) {
                        stringBuilder.append(string);
                    }

                    this.fromString(stringBuilder.toString(), activeMods);
                } catch (Throwable throwable0) {
                    try {
                        bufferedReader.close();
                    } catch (Throwable throwable1) {
                        throwable0.addSuppressed(throwable1);
                    }

                    throw throwable0;
                }

                bufferedReader.close();
            } catch (Throwable throwable2) {
                try {
                    fileReader.close();
                } catch (Throwable throwable3) {
                    throwable2.addSuppressed(throwable3);
                }

                throw throwable2;
            }

            fileReader.close();
            return true;
        } catch (FileNotFoundException fileNotFoundException) {
            return false;
        } catch (Exception exception) {
            ExceptionLogger.logException(exception);
            return false;
        }
    }

    private void fromString(String string0, ActiveMods activeMods) {
        string0 = ScriptParser.stripComments(string0);
        ScriptParser.Block block0 = ScriptParser.parse(string0);
        int _int = -1;
        ScriptParser.Value value0 = block0.getValue("VERSION");
        if (value0 != null) {
            _int = PZMath.tryParseInt(value0.getValue(), -1);
        }

        if (_int >= 1 && _int <= 1) {
            ScriptParser.Block block1 = block0.getBlock("mods", null);
            if (block1 != null) {
                for (ScriptParser.Value value1 : block1.values) {
                    String string1 = value1.getKey().trim();
                    if (string1.equalsIgnoreCase("mod")) {
                        String string2 = value1.getValue().trim();
                        if (!StringUtils.isNullOrWhitespace(string2)) {
                            activeMods.getMods().add(string2);
                        }
                    }
                }
            }

            ScriptParser.Block block2 = block0.getBlock("maps", null);
            if (block2 != null) {
                for (ScriptParser.Value value2 : block2.values) {
                    String string3 = value2.getKey().trim();
                    if (string3.equalsIgnoreCase("map")) {
                        String string4 = value2.getValue().trim();
                        if (!StringUtils.isNullOrWhitespace(string4)) {
                            activeMods.getMapOrder().add(string4);
                        }
                    }
                }
            }
        }
    }
}
